/**
 * Created by vincent on 2017/2/10.
 */
moduleUtils.directive('cmSelect', function () {
    var directive = {};
    directive.restrict = 'A';
    directive.require = '?ngModel';
    directive.scope = {
        options: '=options',
        model: '=ngModel'
    };


    directive.link = function (scope, element, attrs, ngModel) {
        if (!ngModel) {
            return;
        }
        $(element).selectpicker({
            noneSelectedText: "请选择",
            dropupAuto: false,
            liveSearch: true
        });

        $(element).on("change", function (e) {
            var currentVal = $(this).val();
            if (currentVal == "" || null === currentVal) {
                if (!$(this).attr("multiple")) {
                    $(this).val("").find("option").removeAttr("selected").prop("selected", false);
                    scope.$applyAsync(function () {
                        scope.model = null;
                    });
                } else {
                    $(this).val("").find("option").removeAttr("selected").prop("selected", false);
                    scope.$applyAsync(function () {
                        angular.forEach(scope.options, function (opt) {
                            opt.selected = false;
                        });
                    });
                }
            } else {
                if (!$(this).attr("multiple")) {
                    var opt = $(this).find("option[value='" + currentVal + "']");
                    var index = $(this).find("option").index(opt);
                    $(this).find("option[value='" + currentVal + "']").attr("selected", true);
                    $(this).find("option[value!='" + currentVal + "']").attr("selected", false);
                    scope.$applyAsync(function () {
                        scope.model = index > 0 ? scope.options[index - 1] : null;
                    });
                } else {
                    scope.$applyAsync(function () {
                        angular.forEach(scope.options, function (opt) {
                            opt.selected = false;
                        });
                        angular.forEach(currentVal, function (valTemp) {
                            var optTemp = $(element).find("option[value='" + valTemp + "']");
                            var indexTemp = $(element).find("option").index(optTemp);
                            if (indexTemp > 0) {
                                scope.options[indexTemp - 1].selected = true;
                            }
                        });
                    });
                }
            }
        });

        //初始化
        var index = 0;
        if (scope.options) {
            for (var i = 0; i < scope.options.length; i++) {
                if (!StringUtils.isNlOrUndOrEmpty(scope.model) && scope.options[i].id == scope.model.id) {
                    index = i + 1;
                }
            }
        }

        if (!$(element).attr("multiple")) {
            $(element).parent().find(".dropdown-menu li a").eq(index).trigger("click");
            scope.$applyAsync(function () {
                scope.model = index > 0 ? scope.options[index - 1] : null;
            });
        }

        //重置
        scope.$on("init-select", function () {
            if (!$(element).attr("multiple")) {
                $(".bootstrap-select").each(function () {
                    $(element).parent().find(".dropdown-menu li:not(.disabled) a").eq(0).trigger("click");
                });
            }
        });

    };
    return directive;
});